//
//  TMNearbyViewModel.m
//  国学培训用户端
//
//  Created by Tao Li on 2018/8/23.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "TMNearbyViewModel.h"
#import "TMShopCourseModel.h"
#import "TMCommentModel.h"
#import "TMCourseTypeModel.h"
#import "TMSearchModel.h"
#import "TMDiscountModel.h"
#import "TMMyCourseModel.h"
@interface TMNearbyViewModel()
@property (nonatomic, strong) NSMutableArray *teacherList;
@end
@implementation TMNearbyViewModel
- (NSMutableArray *)teacherList {
    if (_teacherList ==nil) {
        _teacherList = [NSMutableArray array];
    }return _teacherList;
}
#pragma --mark 教师详情
- (void)getTeacherDetailWithParam:(NSInteger)tid AndPage:(NSInteger)page Success:(CompleteBlock)block Failed:(FailureBlock)failre{
    if (page == 1) {
        [self.teacherList removeAllObjects];
    }
    NSMutableDictionary *param = [TMUserManager paramDisplay];
    [param setValue:@(tid) forKey:@"id"];
    [param setValue:@(page) forKey:@"page"];
    [[TMDataRequest defaultRequest] requestWithPath:TMAPI_GetTeacherDetail parameters:param method:0 prepareExecute:^{
        
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        TMShopTeacherModel *model = [TMShopTeacherModel yy_modelWithJSON:responseObject[@"list"]];
        model.average = [responseObject[@"data"][@"average"]integerValue];
        model.total = [responseObject[@"data"][@"total"]integerValue];
        model.totalPage = [responseObject[@"totalPage"] integerValue];
        NSMutableArray *arr = [NSMutableArray array];
        for (NSDictionary *dic in responseObject[@"tip"]) {
            TMCommentModel *modess = [TMCommentModel yy_modelWithJSON:dic];
            [arr addObject:modess];
        }
        [self.teacherList addObjectsFromArray:arr];
         block(model,self.teacherList);
    } error:^(NSInteger errorCode, NSString *errorMessage) {
        failre(errorMessage);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        failre(@"1");
    }];
}
#pragma --mark 在职教师
- (void)getWorkingTeacherWithParam:(NSInteger)did Success:(SuccessBlock)block Failed:(FailureBlock)failre{
    [self.teacherList removeAllObjects];
    NSMutableDictionary *param = [TMUserManager paramDisplay];
    [param setValue:@(did) forKey:@"id"];
    [[TMDataRequest defaultRequest] requestWithPath:TMAPI_GetWorkingTeacher parameters:param method:1 prepareExecute:^{
        
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        NSMutableArray *arr = [NSMutableArray array];
        for (NSDictionary *dic in responseObject[@"teacher"]) {
            TMShopTeacherModel *model = [TMShopTeacherModel yy_modelWithJSON:dic];
            [arr addObject:model];
        }
        [self.teacherList addObjectsFromArray:arr];
        block(self.teacherList);
    } error:^(NSInteger errorCode, NSString *errorMessage) {
        failre(errorMessage);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        failre(@"1");
    }];
}
#pragma --mark 评论
- (void)getShopCommentWithParam:(NSInteger)did AndPage:(NSInteger)page Success:(CompleteBlock)block Failed:(FailureBlock)failre{
    if (page == 1) {
        [self.teacherList removeAllObjects];
    }
    NSMutableDictionary *param = [TMUserManager paramDisplay];
    [param setValue:@(did) forKey:@"id"];
    [param setValue:@(page) forKey:@"page"];
    [[TMDataRequest defaultRequest] requestWithPath:TMAPI_GetShopComment parameters:param method:1 prepareExecute:^{
        
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        NSMutableArray *arr = [NSMutableArray array];
        for (NSDictionary *dic in responseObject[@"tip"]) {
            TMCommentModel *modess = [TMCommentModel yy_modelWithJSON:dic];
            modess.total = [responseObject[@"data"][@"total"] integerValue];
            modess.average = [responseObject[@"data"][@"average"] integerValue];
            [arr addObject:modess];

        }
        [self.teacherList addObjectsFromArray:arr];
        block(self.teacherList,responseObject[@"totalPage"]);
    } error:^(NSInteger errorCode, NSString *errorMessage) {
        failre(errorMessage);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        failre(@"1");
    }];
}
#pragma --mark 课程出售
- (void)getShopCourseSaleWithParam:(NSInteger)did Type:(NSInteger)tid Page:(NSInteger)page Success:(CompleteBlock)block Failed:(FailureBlock)failre{
    if (page == 1) {
        [self.teacherList removeAllObjects];
    }
     NSMutableArray *arr = [NSMutableArray array];
    NSMutableDictionary *param = [TMUserManager paramDisplay];
    [param setValue:@(did) forKey:@"id"];
    if (tid != 10000) {
        [param setValue:@(tid) forKey:@"type_id"];
    }
    [param setValue:@(page) forKey:@"page"];
    [[TMDataRequest defaultRequest] requestWithPath:TMAPI_GetCourseSale parameters:param method:1 prepareExecute:^{
        
    } success:^(NSURLSessionDataTask *task, id responseObject) {
         NSMutableArray *arrsss = [NSMutableArray array];
        for (NSDictionary *dic in responseObject[@"type"]) {
            TMCourseTypeModel *modess = [TMCourseTypeModel yy_modelWithJSON:dic];
            [arrsss addObject:modess];
        }
        [arr addObject:arrsss];
        
        //NSMutableArray *arr1 = [NSMutableArray array];
        if (page == 1) {
            [self.teacherList removeAllObjects];
        }
        for (NSDictionary *dic in responseObject[@"course"]) {
            TMShopCourseModel *modess = [TMShopCourseModel yy_modelWithJSON:dic];
            [self.teacherList addObject:modess];
        }
        [arr addObject:self.teacherList];
    block(arr, responseObject[@"total"]);
    } error:^(NSInteger errorCode, NSString *errorMessage) {
        failre(errorMessage);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        failre(@"1");
    }];
}
#pragma --mark 精品
- (void)getBoutiqueCourseWithPage:(NSInteger)page Success:(CompleteBlock)block Failed:(FailureBlock)failre{
    if (page == 1) {
        [self.teacherList removeAllObjects];
    }
    NSMutableDictionary *param = [TMUserManager paramDisplay];
    [param setValue:@(page) forKey:@"page"];
    [[TMDataRequest defaultRequest] requestWithPath:TMAPI_GetBoutiqueCourse parameters:param method:1 prepareExecute:^{
        
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        NSMutableArray *arr = [NSMutableArray array];
        for (NSDictionary *dic in responseObject[@"list"]) {
            TMCourseTypeModel *modess = [TMCourseTypeModel yy_modelWithJSON:dic];
            [arr addObject:modess];
        }
        [self.teacherList addObjectsFromArray:arr];
        
        block(self.teacherList, responseObject[@"total"]);
    } error:^(NSInteger errorCode, NSString *errorMessage) {
        failre(errorMessage);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        failre(@"1");
    }];
}
#pragma --mark 附近课程
- (void)getNearbyCourseWithParam:(NSString *)degree Success:(SuccessBlock)block Failed:(FailureBlock)failre{
    [self.teacherList removeAllObjects];
    NSMutableDictionary *param = [TMUserManager paramDisplay];
    [param setValue:degree forKey:@"degree"];
    [[TMDataRequest defaultRequest] requestWithPath:TMAPI_GetNearbyCourseHouse parameters:param method:1 prepareExecute:^{
        
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        NSMutableArray *arr = [NSMutableArray array];
        for (NSDictionary *dic in responseObject[@"data"]) {
            TMSearchModel *modess = [TMSearchModel yy_modelWithJSON:dic];
            [arr addObject:modess];
        }
        [self.teacherList addObjectsFromArray:arr];

        block(self.teacherList);
    } error:^(NSInteger errorCode, NSString *errorMessage) {
        [TMProgressHUD showErrorWithStyleDarkStatus:errorMessage];
        failre(errorMessage);
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        failre(@"1");
    }];
}
#pragma --mark 课程优惠券
-(void)getCourseDiscountWith:(NSInteger)cid Success:(SuccessBlock)success failed:(FailureBlock)failed{
    [self.teacherList removeAllObjects];
    NSMutableDictionary *param = [TMUserManager paramDisplay];
    [param setObject:@(cid) forKey:@"id"];
    [[TMDataRequest defaultRequest] requestWithPath:TMAPI_GetdiscoupList parameters:param method:1 prepareExecute:^{
        
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"%@", responseObject);
        NSMutableArray *arr = [NSMutableArray array];
        for (NSDictionary *dic in responseObject[@"list"]) {
            TMDiscountModel *model = [TMDiscountModel yy_modelWithJSON:dic];
            [arr addObject:model];
        }
        [self.teacherList addObjectsFromArray:arr];
        success(self.teacherList);
    } error:^(NSInteger errorCode, NSString *errorMessage) {
        failed(errorMessage);
        [TMProgressHUD showErrorWithStyleDarkStatus:errorMessage];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        failed(@"1");
        [TMProgressHUD showErrorWithStyleDarkStatus:@"网络错误！"];
    }];
}
#pragma --mark 提交订单优惠券
-(void)getToUsrDiscountWith:(NSInteger)did Success:(SuccessBlock)success failed:(FailureBlock)failed{
    NSMutableDictionary *param = [TMUserManager paramDisplay];
    [param setObject:@(did) forKey:@"id"];
    [[TMDataRequest defaultRequest] requestWithPath:TMAPI_GetToUseDiscoup parameters:param method:1 prepareExecute:^{
        
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"%@", responseObject);
        [TMProgressHUD showSuccessWithStyleDarkStatus:@"领取成功！"];
        success(responseObject);
    } error:^(NSInteger errorCode, NSString *errorMessage) {
        failed(errorMessage);
        [TMProgressHUD showErrorWithStyleDarkStatus:errorMessage];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        failed(@"1");
        [TMProgressHUD showErrorWithStyleDarkStatus:@"网络错误！"];
    }];
}
#pragma --mark 获取可用优惠券
-(void)getCanUuseDiscountListWith:(NSInteger)did With:(NSInteger)cid Success:(SuccessBlock)success failed:(FailureBlock)failed{
    [self.teacherList removeAllObjects];
    NSMutableDictionary *param = [TMUserManager paramDisplay];
    [param setObject:@(did) forKey:@"id"];
    [param setObject:@(cid) forKey:@"cid"];
    [[TMDataRequest defaultRequest] requestWithPath:TMAPI_GetCoursediscoupList parameters:param method:1 prepareExecute:^{
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"%@", responseObject);
        NSMutableArray *arr = [NSMutableArray array];
        for (NSDictionary *dic in responseObject[@"list"]) {
            TMDiscountModel *model = [TMDiscountModel yy_modelWithJSON:dic];
            [arr addObject:model];
        }
        for (NSDictionary *dic in responseObject[@"data"]) {
            TMDiscountModel *model = [TMDiscountModel yy_modelWithJSON:dic];
            [arr addObject:model];
        }
        [self.teacherList addObjectsFromArray:arr];
        success(self.teacherList);
    } error:^(NSInteger errorCode, NSString *errorMessage) {
        failed(errorMessage);
        [TMProgressHUD showErrorWithStyleDarkStatus:errorMessage];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        failed(@"1");
        [TMProgressHUD showErrorWithStyleDarkStatus:@"网络错误！"];
    }];
}
#pragma --mark 我也不知道是什么了！！！！
-(void)getRealUseDiscountWith:(NSDictionary *)param Success:(SuccessBlock)success failed:(FailureBlock)failed{
    [[TMDataRequest defaultRequest] requestWithPath:TMAPI_GetRealUseDiscoup parameters:param method:1 prepareExecute:^{
        
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"%@", responseObject);
        [TMProgressHUD showSuccessWithStyleDarkStatus:@"使用成功！"];
        success(responseObject);
    } error:^(NSInteger errorCode, NSString *errorMessage) {
        failed(errorMessage);
        [TMProgressHUD showErrorWithStyleDarkStatus:errorMessage];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        failed(@"1");
        [TMProgressHUD showErrorWithStyleDarkStatus:@"网络错误！"];
    }];
}

#pragma --mark 购买课程
-(void)getBuyCourseWith:(NSDictionary *)param Success:(SuccessBlock)success failed:(FailureBlock)failed{
    [self.teacherList removeAllObjects];
    NSMutableDictionary *mut = [TMUserManager paramDisplay];
    [mut addEntriesFromDictionary:param];
    [[TMDataRequest defaultRequest] requestWithPath:TMAPI_GetPayCourse parameters:mut method:1 prepareExecute:^{
        
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"%@", responseObject);
        NSMutableArray *arr = [NSMutableArray array];
        for (NSDictionary *dic in responseObject[@"list"]) {
            TMMyCartModel *model = [TMMyCartModel yy_modelWithJSON:dic];
            model.rank = [responseObject[@"rank"] integerValue];
            [arr addObject:model];
        }
        [self.teacherList addObjectsFromArray:arr];
        success(self.teacherList);
    } error:^(NSInteger errorCode, NSString *errorMessage) {
        failed(errorMessage);
        [TMProgressHUD showErrorWithStyleDarkStatus:errorMessage];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        failed(@"1");
        [TMProgressHUD showErrorWithStyleDarkStatus:@"网络错误！"];
    }];
}
#pragma --mark 提交订单
-(void)CommitOrderWith:(NSDictionary *)param Success:(SuccessBlock)success failed:(FailureBlock)failed{
    [[TMDataRequest defaultRequest] requestWithPath:TMAPI_CommitPayOrder parameters:param method:1 prepareExecute:^{
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"%@", responseObject);
        NSMutableArray *arr = [NSMutableArray array];
//        code = 200;
//        money = 1;
//        msg = "\U6210\U529f";
//        number = DC2018090310153555;
        success(responseObject);
    } error:^(NSInteger errorCode, NSString *errorMessage) {
        failed(errorMessage);
        [TMProgressHUD showErrorWithStyleDarkStatus:errorMessage];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        failed(@"1");
        [TMProgressHUD showErrorWithStyleDarkStatus:@"网络错误！"];
    }];
}
#pragma --mark 课程预定
-(void)getToOrderCourseWith:(NSInteger)cid Success:(SuccessBlock)success failed:(FailureBlock)failed{
    NSMutableDictionary *mut = [TMUserManager paramDisplay];
    [mut setValue:@(cid) forKey:@"id"];
    [[TMDataRequest defaultRequest] requestWithPath:TMAPI_OrderCourseAction parameters:mut method:1 prepareExecute:^{
        
    } success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"%@", responseObject);
        [TMProgressHUD showSuccessWithStyleDarkStatus:@"预约成功！"];
        success(responseObject);
    } error:^(NSInteger errorCode, NSString *errorMessage) {
        failed(errorMessage);
        [TMProgressHUD showErrorWithStyleDarkStatus:errorMessage];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        failed(@"1");
        [TMProgressHUD showErrorWithStyleDarkStatus:@"网络错误！"];
    }];
}
@end
